package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.qf.listener.DepartmentListener;
import com.qf.model.entity.Department;
import com.qf.dao.DepartmentDao;
import com.qf.model.vo.DepartmentVO;
import com.qf.service.DepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (Department)表服务实现类
 *
 * @author ZhiHuiJun
 * @since 2024-05-02 15:03:16
 */
@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Department queryById(Long id) {
        return this.departmentDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Department> queryByPage(Long pageNum) {
        return this.departmentDao.queryAllByLimit();
    }

    @Override
    public List<Department> selectDeptByCondition(Map<String, Object> map) {
        return departmentDao.selectDeptByCondition(map);
    }

    @Override
    public List<Department> selectAllDept() {
        return departmentDao.selectAllDept();
    }

    /**
     * 新增数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Department department) {
        int i = departmentDao.insert(department);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public int insertBatch(List<Department> list) {
        return departmentDao.insertBatch(list);
    }

    /**
     * 修改数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Department department) {
        int i = departmentDao.update(department);
        if (i > 0){
            return true;
        }
        return false;
    }
    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */

    @Override
    public boolean deleteById(Long id) {
        int i = departmentDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteBatch(List<Integer> list) {
        int i = departmentDao.deleteBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void departmentExport(HttpServletResponse response) {
        List<Department> list = departmentDao.selectAllDept();
        try {
            EasyExcel.write(response.getOutputStream(), Department.class)
                    .sheet("科室信息").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean importDepartment(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), Department.class, new DepartmentListener(this))
                    .sheet("科室信息").doReadSync();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

}
